package com.ulook.lookplanet.data.repository;

import android.database.Cursor;
import android.util.Log;

import com.ulook.lookplanet.data.source.NotificationsDataSource;
import com.ulook.lookplanet.ui.notifications.VMsgUser;

import org.litepal.LitePal;

import java.util.List;

/**
 * 描述：
 *
 * @author L1998
 * @data 2021/4/28
 */
public class NotificationsRepository {

    public static NotificationsRepository instance;
    private NotificationsDataSource dataSource;

    private NotificationsRepository(NotificationsDataSource dataSource){
        this.dataSource = dataSource;
    }

    public static NotificationsRepository getInstance(){
        if(instance == null){
            instance = new NotificationsRepository(new NotificationsDataSource());
        }
        return instance;
    }

    /**
     * 获取用户聊天列表
     */
    public List<VMsgUser> getChatList(int toUserId){
        return LitePal.where("(fromuser = ? and touser != ?) or touser = ?", toUserId+"", toUserId+"", toUserId+"").find(VMsgUser.class);
    }

    /**
     * 获取聊天最新一条消息
     */
    public VMsgUser getLatestMsg(int fromUserId, int toUserId){
        List<VMsgUser> list = LitePal.where("fromuser = ? and touser = ?", fromUserId+"", toUserId+"")
                .order("sendtime desc")
                .limit(1)
                .find(VMsgUser.class);
        if(list != null && list.size() > 0){
            Log.d("######Notif-获取聊天最新一条消息", list.get(0).toString());
            return list.get(0);
        }
        return null;
    }

    /**
     * 获取未读消息数量
     */
    public int getUnreadNum(int fromUserId){
        List<VMsgUser> msgUsers = LitePal.select("uid").where("fromuser = " + fromUserId + " and state = 0").find(VMsgUser.class);
        return msgUsers.size();
    }
}
